package com.dormitory.dao.impl;

import com.dormitory.dao.StudentDao;
import com.dormitory.model.Student;
import com.dormitory.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl implements StudentDao {
    
    @Override
    public boolean save(Student student) {
        String sql = "INSERT INTO students (student_name, gender, major, class_name, contact_phone, enrollment_year) " +
                    "VALUES (?, ?, ?, ?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            
            stmt.setString(1, student.getStudentName());
            stmt.setString(2, student.getGender());
            stmt.setString(3, student.getMajor());
            stmt.setString(4, student.getClassName());
            stmt.setString(5, student.getContactPhone());
            stmt.setInt(6, student.getEnrollmentYear());
            
            return stmt.executeUpdate() > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean update(Student student) {
        String sql = "UPDATE students SET student_name=?, gender=?, major=?, class_name=?, " +
                    "contact_phone=?, enrollment_year=? WHERE student_id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            
            stmt.setString(1, student.getStudentName());
            stmt.setString(2, student.getGender());
            stmt.setString(3, student.getMajor());
            stmt.setString(4, student.getClassName());
            stmt.setString(5, student.getContactPhone());
            stmt.setInt(6, student.getEnrollmentYear());
            stmt.setInt(7, student.getStudentId());
            
            return stmt.executeUpdate() > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean delete(Integer studentId) {
        String sql = "DELETE FROM students WHERE student_id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            
            stmt.setInt(1, studentId);
            return stmt.executeUpdate() > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Student findById(Integer studentId) {
        String sql = "SELECT * FROM students WHERE student_id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            
            stmt.setInt(1, studentId);
            ResultSet rs = stmt.executeQuery();
            
            if (rs.next()) {
                return extractStudentFromResultSet(rs);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Student> findAll() {
        String sql = "SELECT * FROM students";
        List<Student> students = new ArrayList<>();
        
        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            
            while (rs.next()) {
                students.add(extractStudentFromResultSet(rs));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return students;
    }

    @Override
    public List<Student> findByCondition(String condition) {
        String sql = "SELECT * FROM students WHERE student_name LIKE ? OR major LIKE ? OR class_name LIKE ?";
        List<Student> students = new ArrayList<>();
        
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            
            String searchPattern = "%" + condition + "%";
            stmt.setString(1, searchPattern);
            stmt.setString(2, searchPattern);
            stmt.setString(3, searchPattern);
            
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                students.add(extractStudentFromResultSet(rs));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return students;
    }

    private Student extractStudentFromResultSet(ResultSet rs) throws SQLException {
        Student student = new Student();
        student.setStudentId(rs.getInt("student_id"));
        student.setStudentName(rs.getString("student_name"));
        student.setGender(rs.getString("gender"));
        student.setMajor(rs.getString("major"));
        student.setClassName(rs.getString("class_name"));
        student.setContactPhone(rs.getString("contact_phone"));
        student.setEnrollmentYear(rs.getInt("enrollment_year"));
        return student;
    }
} 